﻿#pragma once
#ifndef NetworkDef_hpp
#define NetworkDef_hpp

#include<stdint.h>
#include<vector>
#include<cstring>


namespace MMFNetwork{


    //因特网版本
    enum class MMFInetFamily
    {
        V4,
        V6
    };

    enum class MMFMesaageType
    {
        Bin,
        ProtoBuf,     
    };

    //这里的数据是在4字节边界上的
    //正式的数据会在mName的后面
    //这里使用柔性数组
    #ifdef _MSC_VER
    #pragma warning(disable : 4200) //干掉警告
    #endif
    struct MMFMessageHeader
    {
        MMFMesaageType mType;       ///< 消息类型
        uint32_t mDataBytes;        ///< 数据总字节数
        uint32_t mNameBytes;        ///< 数据协议名字长度
        char  mName[0];             ///< 数据协议名字
    };

    inline uint32_t GetMessageHeadLen(MMFMessageHeader* head)
    {
        return sizeof(MMFMessageHeader) + head->mNameBytes;
    }

    inline uint32_t CalcMessageHeadLen(const char* msgName)
    {
        uint32_t nameLen = (uint32_t)strlen(msgName) + 1;
        return (uint32_t)sizeof(MMFMessageHeader) + nameLen;
    }

    inline uint32_t FillMessageHead(std::vector<char>& buff, uint32_t headLen, const char* msgName, uint32_t dataBytes)
    {
        uint32_t nameLen = headLen - (uint32_t)sizeof(MMFMessageHeader);
        uint32_t totalLen = headLen + dataBytes;
        MMFMessageHeader* head = (MMFMessageHeader*)buff.data();
        head->mNameBytes = nameLen;
        head->mDataBytes = totalLen;
        memcpy(head->mName, msgName, nameLen);
        return totalLen;
    }

    //session的状态枚举
    enum class Status
    {
        ReadErr = -5,
        WriteErr = -4,
        AsyncReadErr = -3,
        AsyncWriteErr = -2,
        Invalid = -1,
        Connected
    };

    //网络消息最大支持的字节数
    const uint32_t MAX_MESSAGE_BUFFER_SIZE = 1024 * 2048;      
}



#endif